package com.atguigu.yygh.hosp.controller.api;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.util.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @FileName: ApiController
 * @Author Steven
 * @Date: 2021/3/18
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("/api/hosp")
public class ApiController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    DepartmentService departmentService;

    @Autowired
    HospitalSetService hospitalSetService;

    @Autowired
    ScheduleService scheduleService;

    /**
     * 查询医院接口
     * @param
     * @return
     */

    @ApiOperation("显示医院信息")
    @PostMapping("hospital/show")
    public Result hospital(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String md5Sign = MD5.encrypt(signKey);
        String hospSign = (String)paramMap.get("sign");
        if(!hospSign.equals(md5Sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation("上传医院")
    @PostMapping("/saveHospital")
    public Result saveHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        //获取医院信息传递过来的签名信息
        String hospSign = (String)paramMap.get("sign");
        //签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String md5Sign = MD5.encrypt(signKey);
        if(!hospSign.equals(md5Sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        hospitalService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        //获取医院信息传递过来的签名信息
        String hospSign = (String)paramMap.get("sign");
        //签名校验
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        String md5Sign = MD5.encrypt(signKey);
        if(!hospSign.equals(md5Sign)){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("分页查询部门信息")
    @PostMapping("/department/list")
    public Result department(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hascode = (String) paramMap.get("hascode");
        String depcode = (String) paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 :
                Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 :
                Integer.parseInt((String) paramMap.get("limit"));
        DepartmentQueryVo queryVo = new DepartmentQueryVo();
        queryVo.setHoscode(hascode);
        queryVo.setDepcode(depcode);
        Page<Department> departmentPage = departmentService.selectPage(page, limit, queryVo);
        return Result.ok(departmentPage);
    }

    @ApiOperation("删除部门信息")
    @PostMapping("/department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hosCode = (String) paramMap.get("hoscode");
        String depCode = (String) paramMap.get("depcode");
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        departmentService.remove(hosCode, depCode);
        return Result.ok();
    }

    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hosCode=(String) paramMap.get("hoscode");
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoDataString = (String) paramMap.get("logoData");
        if (!StringUtils.isEmpty(logoDataString)) {
            String logoData = logoDataString.replaceAll(" ", "+");
            paramMap.put("logoData", logoData);
        }
        if(StringUtils.isEmpty(hosCode)){
            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
        }
        if(!HttpRequestHelper.isSignEquals(paramMap,hospitalSetService.getSignKey(hosCode))){
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("获取排版分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hosCode=(String)paramMap.get("hoscode");
        String deptCode=(String) paramMap.get("deptcode");
        int page=StringUtils.isEmpty(paramMap.get("page"))? 1: Integer.parseInt((String)paramMap.get("page"));
        int limit=StringUtils.isEmpty(paramMap.get("limit")) ? 10: Integer.parseInt((String)paramMap.get("limit"));
        ScheduleQueryVo queryVo=new ScheduleQueryVo();
        queryVo.setHoscode(hosCode);
        queryVo.setDepcode(deptCode);
        Page<Schedule> pages = scheduleService.selectPage(page, limit, queryVo);
        return Result.ok(pages);
    }

    @ApiOperation("删除科室")
    @PostMapping("schedule/remove")
    public Result remove(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hosCode=(String)paramMap.get("hoscode");
        String hosScheduleId=(String)paramMap.get("hosScheduleId");
//        if(StringUtils.isEmpty(hosCode)){
//            throw new YyghException(ResultCodeEnum.PARAM_ERROR);
//        }
//        if(HttpRequestHelper.isSignEquals(paramMap,hospitalSetService.getSignKey(hosCode))){
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        scheduleService.remove(hosCode,hosScheduleId);
        return Result.ok();
    }

}
